#include "robotsettingdlg.h"
#include "ui_robotsettingdlg.h"
#include <qmessagebox.h>

#include "../CommunicateDelegate/communicatenormaldelegate.h"

RobotSettingDlg::RobotSettingDlg(int styleId,CommunicateNormalDelegate *communicateNormalIn,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RobotSettingDlg)
{
    ui->setupUi(this);
    communicateNormal=communicateNormalIn;
//    setGeometry(70,128,460,595);
//    move(70,128);
//    this->setWindowFlags(Qt::Sheet |Qt::FramelessWindowHint);
//    this->setWindowFlags(Qt::FramelessWindowHint);    //无边框
//    this->setStyleSheet("outline: none;");            //无虚线框
//    setWindowFlags( Qt::FramelessWindowHint | Qt::Tool );

    setWindowFlags(Qt::FramelessWindowHint);
    setStyleSheet("background-color:white;");
    //    setGeometry(70,128,460,595);
    move(0,0);
    initialDisplayStyle(styleId);
    hide();
}

void RobotSettingDlg::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);
        ui->pushButton_hide->setGeometry(480,400,100,40);
        break;
    default:
        break;
    }
}

RobotSettingDlg::~RobotSettingDlg()
{
    delete ui;
}

void RobotSettingDlg::on_pushButton_read_sdo_clicked()
{
    bool isOk;
    int rObIndex=ui->lineEdit_mainIndex->text().toInt(&isOk,16);
    int rObSubIndex=ui->lineEdit_subIndex->text().toInt(&isOk,16);
    LONG64 rData=-1;
    int rDataLen=4;
    int motorIndex=ui->lineEdit_motorIndex->text().toInt();
    int robotId=ui->lineEdit_robotId->text().toInt();
    rDataLen=communicateNormal->getSdo(robotId,motorIndex,rObIndex,rObSubIndex,rData);

    if(0<rDataLen)
    {
        ui->lineEdit_length->setText(QString::number(rDataLen));
        QString tmp;
        tmp.clear();
        tmp.sprintf("%02x",rData);
        ui->lineEdit_value->setText(tmp);
       QMessageBox::information(NULL, "read sdo success成功!", "成功", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "read sdo failed 失败!", "失败", QMessageBox::Yes );
    }
}

void RobotSettingDlg::on_pushButton_write_sdo_clicked()
{
    bool isOk;
    int rObIndex=ui->lineEdit_mainIndex->text().toInt(&isOk,16);
    int rObSubIndex=ui->lineEdit_subIndex->text().toInt(&isOk,16);
    LONG64 rData=ui->lineEdit_value->text().toLongLong(&isOk,16);
    int rDataLen=ui->lineEdit_length->text().toInt();
    int motorIndex=ui->lineEdit_motorIndex->text().toInt();
    int returnKey;
    int robotId=ui->lineEdit_robotId->text().toInt();
    communicateNormal->setSdo(robotId,motorIndex
                                   ,rObIndex,rObSubIndex,rData,rDataLen,returnKey);
    if(1==returnKey)
    {
       QMessageBox::information(NULL, "write sdo success成功!", "成功", QMessageBox::Yes );
    }
    else
    {
        QMessageBox::information(NULL, "writesdo failed 失败!", "失败", QMessageBox::Yes );
    }
}

void RobotSettingDlg::on_pushButton_hide_clicked()
{
    hide();
}
